package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
@Transactional
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper RoomInfoMapper;
    @Autowired
    private ApartmentInfoMapper  apartmentInfoMapper;
    @Autowired
    private AttrValueMapper  attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper  labelInfoMapper;
    @Autowired
    private PaymentTypeMapper  paymentTypeMapper;
    @Autowired
    private LeaseTermMapper  leaseTermMapper;
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {


        //若为更新操作，则先删除与Room相关的各项信息列表
        if (roomSubmitVo.getId() != null) {
            LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId())
                    .eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoWrapper);

            LambdaQueryWrapper<RoomAttrValue> roomAttrValueWrapper = new LambdaQueryWrapper<>();
            roomAttrValueWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueWrapper);

            //3.删除原有roomFacilityList
            LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
            facilityQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(facilityQueryWrapper);

            //4.删除原有roomLabelList
            LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(labelQueryWrapper);

            //5.删除原有paymentTypeList
            LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
            paymentQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(paymentQueryWrapper);


            //6.删除原有leaseTermList
            LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
            termQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(termQueryWrapper);
        }
        this.saveOrUpdate(roomSubmitVo);
        Long id = roomSubmitVo.getId();
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemId(id);
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfoList.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfoList);
        //2.保存新的attrValueList
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
        attrValueIds.forEach(attrId -> {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                    .roomId(roomSubmitVo.getId())
                    .attrValueId(attrId)
                    .build();
            roomAttrValueList.add(roomAttrValue);
        });
        roomAttrValueService.saveBatch(roomAttrValueList);
        //3.保存新的facilityInfoList
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> facilityInfoArrayList = new ArrayList<>(facilityInfoIds.size());
        facilityInfoIds.forEach(facilityId -> {
            RoomFacility facilityInfo = RoomFacility.builder()
                    .roomId(id)
                    .facilityId(facilityId)
                    .build();
            facilityInfoArrayList.add(facilityInfo);
        });
        roomFacilityService.saveBatch(facilityInfoArrayList);

        //4.保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> labelInfoArrayList = new ArrayList<>(labelInfoIds.size());
        labelInfoIds.forEach(labelId -> {
            RoomLabel roomLabel = RoomLabel.builder()
                    .roomId(id)
                    .labelId(labelId)
                    .build();
            labelInfoArrayList.add(roomLabel);
        });
        roomLabelService.saveBatch(labelInfoArrayList);

        //5.保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> paymentTypeArrayList = new ArrayList<>(paymentTypeIds.size());
        paymentTypeIds.forEach(paymentId -> {
            RoomPaymentType paymentType = RoomPaymentType.builder()
                    .roomId(id)
                    .paymentTypeId(paymentId)
                    .build();
            paymentTypeArrayList.add(paymentType);
        });
        roomPaymentTypeService.saveBatch(paymentTypeArrayList);

        //6.保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> leaseTermArrayList = new ArrayList<>(leaseTermIds.size());
        leaseTermIds.forEach(leaseId -> {
            RoomLeaseTerm leaseTerm = RoomLeaseTerm.builder()
                    .roomId(id)
                    .leaseTermId(leaseId)
                    .build();
            leaseTermArrayList.add(leaseTerm);
        });
        roomLeaseTermService.saveBatch(leaseTermArrayList);

    }

    @Override
    public IPage<RoomItemVo> findPageList(long current, long size, RoomQueryVo queryVo) {
        //自定义分页的api
        IPage<RoomItemVo> page = new Page<>(current, size);
        page = RoomInfoMapper.findPageList(page, queryVo);
        return page;
    }

    @Override
    public RoomDetailVo findDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //1.查询房间信息
        RoomInfo roomInfo = this.getById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);

        //3.查询图片信息
        List<GraphVo>  graphVoList = graphInfoService.findGraphInfoByItemId(id, ItemType.ROOM);
        roomDetailVo.setGraphVoList(graphVoList);

        //4.获取属性列表信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.findListByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        //5.查询配套信息
        List<FacilityInfo>  facilityInfoList = facilityInfoMapper.findListByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        //6.查询标签信息
        List<LabelInfo>  labelInfoList = labelInfoMapper.findListByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);

        //7.查询租金信息
        List<PaymentType>  paymentTypeList = paymentTypeMapper.findListByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        //8.查询租期信息
        List<LeaseTerm>  leaseTermList = leaseTermMapper.findListByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        //1.删除RoomInfo
        super.removeById(id);

        //2.删除graphInfoList
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //3.删除attrValueList
        LambdaQueryWrapper<RoomAttrValue> attrQueryWrapper = new LambdaQueryWrapper<>();
        attrQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(attrQueryWrapper);

        //4.删除facilityInfoList
        LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(facilityQueryWrapper);

        //5.删除labelInfoList
        LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(labelQueryWrapper);

        //6.删除paymentTypeList
        LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
        paymentQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(paymentQueryWrapper);

        //7.删除leaseTermList
        LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
        termQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(termQueryWrapper);
    }
}




